package org.numb.springbootzookeeper.config;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.numb.springbootzookeeper.prop.ZkProp;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@EnableConfigurationProperties(ZkProp.class)
public class ZkConfiguration {

    @Bean
    public CuratorFramework getZkProp(ZkProp zkProp) throws InterruptedException {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(Integer.parseInt(zkProp.getSleepTimeMs()),
            Integer.parseInt(zkProp.getMaxRetries()));
        CuratorFramework zkClient = CuratorFrameworkFactory.newClient(zkProp.getAddress(), retryPolicy);
        zkClient.start();
        zkClient.blockUntilConnected();
        return zkClient;
    }

}
